﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

public enum TrapKind
{
    None,
    Easy,
    Medium,
    Hard,
    Timer
}

public class Trap : PuzzleObject
{
    private TrapKind trapKind;

    public TrapKind TrapKind
    {
        get { return trapKind; }
    }

    private int paintNumber;
    public int PaintNumber
    {
        get
        {
            if (!state && trapKind == TrapKind.Timer)
                return (int)kind + 4;
            if (state)
                    return (int)kind + 16;
            if (!state && trapKind != TrapKind.Timer)
                return (int)kind;

            return 0;
        }
    }

    private double lastStateChange = 0;

    public Trap(TrapKind kind, int xposition, int yposition)
        : base(ObjectKind.TrapDown, xposition, yposition, false)
    {
        this.trapKind = kind;

        lastStateChange = 0;
    }

    public void Update(GameTime gameTime)
    {
        //Console.WriteLine(lastStateChange +" "+ gameTime.TotalRealTime.TotalMilliseconds);

        if (trapKind == TrapKind.Timer)
        {
            bool stayActive = false;

            if (state)
            {
                foreach (Characters chara in GameSession.CharactersList)
                {
                    if (chara.Position.X / 64 == xposition && chara.Position.Y == yposition)
                    {
                        stayActive = true;
                        break;
                    }
                }
            }

            if (!stayActive)
                state = false; 
        }
        else
        {
            if(lastStateChange == 0 )
                lastStateChange = Globals.GetRandomDouble * 2000 + gameTime.TotalGameTime.TotalMilliseconds;

            switch (trapKind)
            {
                case TrapKind.Easy:
                    if (!state && lastStateChange + 4000 <= gameTime.TotalGameTime.TotalMilliseconds)
                    {
                        lastStateChange = gameTime.TotalGameTime.TotalMilliseconds;
                        state = true;
                    }
                    else
                        if (state && lastStateChange + 2000 <= gameTime.TotalGameTime.TotalMilliseconds)
                        {
                            lastStateChange = gameTime.TotalGameTime.TotalMilliseconds;
                            state = false;
                        }
                    break;
                case TrapKind.Medium:
                    if (!state && lastStateChange + 3000 <= gameTime.TotalGameTime.TotalMilliseconds)
                    {
                        lastStateChange = gameTime.TotalGameTime.TotalMilliseconds;
                        state = true;
                    }
                    else
                        if (state && lastStateChange + 2000 <= gameTime.TotalGameTime.TotalMilliseconds)
                        {
                            lastStateChange = gameTime.TotalGameTime.TotalMilliseconds;
                            state = false;
                        }
                    break;
                case TrapKind.Hard:
                    if (!state && lastStateChange + 2000 <= gameTime.TotalGameTime.TotalMilliseconds)
                    {
                        lastStateChange = gameTime.TotalGameTime.TotalMilliseconds;
                        state = true;
                    }
                    else
                        if (state && lastStateChange + 1500 <= gameTime.TotalGameTime.TotalMilliseconds)
                        {
                            lastStateChange = gameTime.TotalGameTime.TotalMilliseconds;
                            state = false;
                        }
                    break;
            }
        }
    }

}
